﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;

using PlanningGIS.ArcGIS;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>   
    /// 作用：鹰眼图层设置
    /// 作者：
    /// 编写日期：2009-07-07   
    /// </summary>
    public partial class OverViewTreeView : DevComponents.DotNetBar.Office2007Form
    {
        /// <summary>
        /// 
        /// </summary>
        public OverViewTreeView()
        {
            InitializeComponent();
            this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="control"></param>
        public void SetBuddy(object control)
        {
            this.axTOCControl1.SetBuddyControl(control);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            try
            {
                this.treeView1.AfterCheck -= new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
                if (this.treeView1.SelectedNode != null)
                {
                    ChildNodeChange(this.treeView1.SelectedNode);
                    ParentNodeChange(this.treeView1.SelectedNode);
                }
                this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }
        }

        /// <summary>
        /// 更改父节点状态
        /// </summary>
        /// <param name="node"></param>
        private void ParentNodeChange(TreeNode node)
        {
            if (node.Parent != null)
            {
                bool flag = true;
                for (int i = 0; i < node.Parent.Nodes.Count; i++)
                {
                    if (!node.Parent.Nodes[i].Checked)
                        flag = false;
                }
                node.Parent.Checked = flag;
                ParentNodeChange(node.Parent);
            }
        }

        /// <summary>
        /// 更新子节点状态
        /// </summary>
        /// <param name="node"></param>
        private void ChildNodeChange(TreeNode node)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].Checked = node.Checked;
                ChildNodeChange(node.Nodes[i]);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            List<IFeatureLayer> pFeatLyrs = MapHelper.GetAllLayer<IFeatureLayer>((axTOCControl1.Buddy as IMapControl3).Map);
            foreach (IFeatureLayer pFeatLyr in pFeatLyrs)
            {
                pFeatLyr.Visible = true;
            }
            (axTOCControl1.Buddy as IMapControl3).Refresh(esriViewDrawPhase.esriViewGeography, null, null);
            //axTOCControl1.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUnSelectAll_Click(object sender, EventArgs e)
        {
            List<IFeatureLayer> pFeatLyrs = MapHelper.GetAllLayer<IFeatureLayer>((axTOCControl1.Buddy as IMapControl3).Map);
            foreach (IFeatureLayer pFeatLyr in pFeatLyrs)
            {
                pFeatLyr.Visible = false;
            }
            (axTOCControl1.Buddy as IMapControl3).Refresh(esriViewDrawPhase.esriViewGeography, null, null);
            //axTOCControl1.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            this.treeView1.SelectedNode = this.treeView1.HitTest(e.X, e.Y).Node;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
        {
            if (e.button == 2)
            {
                ContextMenuStrip pContextMenu = new ContextMenuStrip();
                ToolStripMenuItem pItemSelectAll = new ToolStripMenuItem("全部选中");

                pContextMenu.Items.Add(pItemSelectAll);
                ToolStripMenuItem pItemUnSelectAll = new ToolStripMenuItem("全部取消");

                pContextMenu.Items.Add(pItemUnSelectAll);


                pItemSelectAll.Click += new EventHandler(btnSelectAll_Click);
                pItemUnSelectAll.Click += new EventHandler(btnUnSelectAll_Click);


                pContextMenu.Show(this.axTOCControl1, e.x, e.y);

            }
        }
    }
}
